<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <base href="<%=basePath%>" />    
    <title></title> 
    <link href="<%=basePath%>css/template.css" rel="stylesheet" type="text/css" />
    <script language="javascript" type="text/javascript" src="<%=basePath%>js/datepicker/WdatePicker.js"></script>
    <script type="text/javascript" language="javascript">
    	function findByList(){
    		document.searchform.action="material!findByList.shtml"
    		document.searchform.submit();
    	}
    	function deleteObject(tempid){
    		if(!confirm("确认删除吗? ")){
    			return;
    		}    		
    		var matid=document.getElementById("matid");    		
    		matid.value=tempid    			
    		document.searchform.action="material!deleteObject.shtml"
    		document.searchform.submit();
    	}
		function findById(tempid,tempmattypeid){
    		var matid = document.getElementById("matid");
    		matid.value=tempid;
    		var mattypeid = document.getElementById("mattypeid");
    		mattypeid.value=tempmattypeid;        				
    		document.searchform.action="material!updateObjectInit.shtml";    	
    		document.searchform.submit();
		}  
		function deleteObjectAll(){
			if(!confirm("确认删除选择的素材吗? ")){
    			return;
    		}
			if(arraycheck.length>0){
				var roleIdStrTemp="";
		 		for(var i = 0 ;i<arraycheck.length;i++){
		 			roleIdStrTemp+=arraycheck[i]+","
		 		}
		 		var delstr = document.getElementById("delstr");
    			delstr.value=roleIdStrTemp.substring(0,roleIdStrTemp.length-1);;
    			document.searchform.action="material!deleteObjectAll.shtml"
    			document.searchform.submit();
			}else{
				alert("未选择用户");
				return false;
			}
		}
		var matsubArray=new Array();    	
    	<c:forEach items="${requestScope.matsublist}" var="matsublist" varStatus="vs">
			matsubArray[${vs.index}] = new Array("${matsublist.matsubname}","${matsublist.matsubid}","${matsublist.mattypeid}");
    	</c:forEach>
    	 
    	function changeMatSubId(mattypeid){
    		if(matsubArray.length==0){
    			return;
    		}
    		var matsubid = document.getElementById('matsubid');
    		if(matsubid==-1){
    			matsubid.length=1;			
    		}else{
    			matsubid.length=1;
    			for(var i=0;i<matsubArray.length;i++){
    				var str=matsubArray[i][2];    				
    				var t=str.split(',');
    				for(var j=0;j<t.length;j++){    			
						if(t[j]==mattypeid){
							matsubid.options[matsubid.length]=new Option(matsubArray[i][0],matsubArray[i][1]);
						}
					}
    			}
    		}
    		if(matsubid.length>0){
    			for(var j = 0 ;j<matsubid.length;j++){
    				if(matsubid.options[j].value=="${requestScope.matsubid}"){
    					matsubid.options[j].selected=true;
    				}
    			}
    		}
    	}
    	
    	function selectAllChk(e) { 
		    var boxs = document.getElementsByName('chk'); 		  
		    for(var i = 0; i < boxs.length; i++){
		   		boxs[i].checked = e.checked; 
		   		chooseRole(boxs[i]);   		      	   
		    }
		}
		
		//多项选择checkbox开始
		function chooseRole(obj){
			OnclickBox(obj);
		}
		var arraycheck=new Array();
		function OnclickBox(obj){
			if(obj.checked){	
				arraycheck[arraycheck.length]=obj.value;	
				var tagname=obj.name;
			}else{
				if(Arraycontains(arraycheck,obj.value)){
				arraycheck.splice(getindex(arraycheck,obj.value),1);
				}
			}			  	
		}		
		function Arraycontains(arraychecks,chvalue){
			var falgs=false;
			for(var i=0;i<arraychecks.length;i++){
			if(arraychecks[i]==chvalue){
				falgs=true;
				break;
				}
			}
		return falgs;
		}
		function getindex(array,objvalue){
			var index;
			for(var i=0;i<array.length;i++){
			if(array[i]==objvalue){
				index=i;
				break;
				}
			}
			return index;
		}
		//多项选择checkbox结束
		function getCount(){
		       var items=document.getElementById("delstr").value;
		       if(items!=""){
		       		var str = items.split(",");
		       		for(var i = 0;i<str.length;i++){
		       			arraycheck[arraycheck.length]=str[i];	
		       			if(document.getElementById(str[i])!=null){
		       				document.getElementById(str[i]).checked="true";
		       			}
		       		}
		       }
    	}    	
    	function goUrl(aurl){
    		if(arraycheck.length>0){
				var roleIdStrTemp="";
		 		for(var i = 0 ;i<arraycheck.length;i++){
		 			roleIdStrTemp+=arraycheck[i]+","
		 		}
		 		var delstr = document.getElementById("delstr");
	   			delstr.value=roleIdStrTemp.substring(0,roleIdStrTemp.length-1);
	   			aurl+="&delstr="+delstr.value;   		      			
			}					
    		window.location.href=aurl;    		
    	}
    </script>   	
  </head>
  
  <body onload="changeMatSubId(${requestScope.mattypeid });getCount();">
    <ul class="menuuser">
  		<li style="font:bold 12px arial;color:#fff;height:20px;line-height:20px;">请选择</li>
  		<c:forEach items="${sessionScope.menulist}" var="menustrlist">
			<c:if test="${menustrlist.menuid==90}">
				<li><a href="material!saveObjectTxtInit.shtml"">添加文字</a></li>
			</c:if>
		</c:forEach>  		
  		<c:forEach items="${sessionScope.menulist}" var="menustrlist">
			<c:if test="${menustrlist.menuid==91}">
				<li><a href="material!saveObjectPicInit.shtml">添加图片</a></li>
			</c:if>
		</c:forEach>
  		<c:forEach items="${sessionScope.menulist}" var="menustrlist">
			<c:if test="${menustrlist.menuid==92}">
				<li><a href="material!saveObjectRingInit.shtml">添加铃音</a></li>
			</c:if>
		</c:forEach>  		
  		<li><a href="material!findByList.shtml">查询素材</a></li>  			  		
  	</ul>
  	<br />
  	<form action="" method="post" name="searchform" id="searchform">
  	<input type="hidden" name="matid" id="matid" />
  	<input type="hidden" name="delstr" id="delstr" value="${requestScope.delstr }" />
  	
  	<table cellpadding="0" cellspacing="0" class="right_table" width="100%" align="center">									
		<tr class="trcss">
			<td>查询条件</td>									
		</tr>
		<tr>
			<td>
				素材名称：
				<input type="text" class="textcss" name="matname" id="matname" value="${requestScope.matname }" />
				
				素材类型：
				<select name="mattypeid" id="mattypeid" onchange="return changeMatSubId(this.value)">
					<option value="-1">全部</option>
					<option value="1" <c:if test="${requestScope.mattypeid==1 }">selected</c:if>>文字</option>
					<option value="2" <c:if test="${requestScope.mattypeid==2 }">selected</c:if>>图片</option>
					<option value="3" <c:if test="${requestScope.mattypeid==3 }">selected</c:if>>铃音 </option>
  				</select>
  				子类型：
  				<select id="matsubid" name="matsubid">
					<option value="-1">请选择</option>
				</select>				
				<br />
				开始时间：
				<input type="text" name="mataddtime" id="mataddtime" style="cursor:hand;border:1px solid #93A4AF;width:150px;height:16px;background:#fff;color:#000;" onFocus="WdatePicker({startDate:'%y-%M-%D 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})" value="${requestScope.mataddtime }"/>
				结束时间：
				<input type="text" name="matdeltime" id="matdeltime" style="cursor:hand;border:1px solid #93A4AF;width:150px;height:16px;background:#fff;color:#000;" onFocus="WdatePicker({startDate:'%y-%M-%D 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})" value="${requestScope.matdeltime }"/>
				<input type="button" value="查 询" class="btn" onclick="return findByList();"/>
				<input type="button" value="删除选择项" class="btn" onclick="return deleteObjectAll();"/>
			</td>			
		</tr>
	</table>   
   	</form>
   	<table cellpadding="0" cellspacing="0" border="0" width="100%" class="right_table">   		
   		<tr class="trcss">
   				<td><input type="checkbox" name="controlchk" id="controlchk" onclick="return selectAllChk(this)"/></td>  				
  				<td>序号ID</td>
  				<td>素材名称</td>  				
  				<td>添加时间</td>
  				<td>素材类型</td>
  				<td>子类型</td>
  				<td>操作</td>
  			</tr>
  			<c:forEach items="${requestScope.matlist}" var="matlist">
  			<tr>
  				<td><input type="checkbox" name="chk" id="${matlist.matid }" value="${matlist.matid }" onclick="chooseRole(this)" /></td>  				
  				<td>${matlist.matid }&nbsp;</td>
  				<td>${matlist.matname }&nbsp;</td>
  				<td>${fn:substring(matlist.mataddtime, 0, 19)}&nbsp;</td>
  				<td>	
  					<c:if test="${matlist.mattypeid==1 }">文字</c:if>
  					<c:if test="${matlist.mattypeid==2 }">图片</c:if>
  					<c:if test="${matlist.mattypeid==3 }">铃音</c:if>&nbsp;
  				</td>
  				<td>
  					<c:forEach items="${requestScope.matsublist}" var="matsublist">
  						<c:if test="${matlist.matsubid==matsublist.matsubid }">${matsublist.matsubname }</c:if>
  					</c:forEach>&nbsp;
  				</td>  				
  				<td>
  					<c:forEach items="${sessionScope.menulist}" var="menustrlist">
						<c:if test="${menustrlist.menuid==93}">
							<a href="javascript:deleteObject(${matlist.matid });">删除</a>&nbsp;&nbsp;&nbsp;
						</c:if>
					</c:forEach>
  					<c:forEach items="${sessionScope.menulist}" var="menustrlist">
						<c:if test="${menustrlist.menuid==94}">
							<a href="javascript:findById(${matlist.matid },${matlist.mattypeid });">查看更新</a>
						</c:if>
					</c:forEach>  					
  				</td>  				
  			</tr>
  			</c:forEach>
   		<tr>
			<td colspan="7" align="right">
					<pg:pager
				    url="material!findByList.shtml"
				    items="${requestScope.pager_count}"
				    index="center"
				    maxPageItems="10"
				    maxIndexPages="5"
				    isOffset="<%= true %>"
				    export="offset,currentPageNumber=pageNumber"
				    scope="request">			
					<pg:param name="index"/>
					<pg:param name="maxPageItems"/>
					<pg:param name="maxIndexPages"/>
					<pg:param name="matname" value="${requestScope.matname }"/>					
					<pg:param name="mataddtime" value="${requestScope.mataddtime }"/>
					<pg:param name="matdeltime" value="${requestScope.matdeltime }"/>
					<pg:param name="mattype" value="${requestScope.mattype }"/>
					<pg:param name="matsubid" value="${requestScope.matsubid }"/>																		  			
					<pg:index>
					总共${requestScope.pager_count}条记录
					<pg:first unless="current">
						<a href="javascript:void(0);" onclick="goUrl('<%=pageUrl %>');">首页</a>
					</pg:first>
					<pg:prev export="prevPageUrl=pageUrl">
					  	<a href="javascript:void(0);" onclick="goUrl('<%= prevPageUrl %>');">前一页</a>
					</pg:prev>
					<pg:pages>
		   				<a href="javascript:void(0);" onclick="goUrl('<%= pageUrl %>');"><%= pageNumber %></a> 
					</pg:pages>
					<pg:next export="nextPageUrl=pageUrl">
					  	<a href="javascript:void(0);" onclick="goUrl('<%= nextPageUrl %>');">下一页</a>
					</pg:next>
					<pg:last>
					  	<a href="javascript:void(0);" onclick="goUrl('<%=pageUrl %>');">末页</a>
					</pg:last>
					</pg:index>
			  		</pg:pager>
			</td>
			</tr>
   	</table>
  </body>
</html>
